package com.asset.mapper;

import com.asset.pojo.AssetDepreciation;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.jdbc.SQL;
import org.apache.ibatis.mapping.FetchType;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

/**
 * 折旧记录访问接口
 *
 * @author 11725
 */
public interface AssetDeprMapper extends Mapper<AssetDepreciation> {
    /**
     * 连接查询，获取匹配的折旧信息集合
     *
     * @param depaId 部门编号
     * @param year   年份
     * @param month  月
     * @return 结果集
     */
    @SelectProvider(type = AssetDeprMapperProvider.class, method = "selectRecordDetail")
    @Results({
            @Result(property = "deprDate", column = "dapr_date"),
            @Result(property = "deprAmount", column = "depr_amount"),
            @Result(property = "asset", column = "asset_id",
                    one = @One(select = "com.asset.mapper.AssetMapper.selectByPrimaryKey", fetchType = FetchType.EAGER))
    })
    List<AssetDepreciation> selectRecordDetail(
            @Param("depaId") Long depaId,
            @Param("year") Integer year,
            @Param("month") Integer month);

    class AssetDeprMapperProvider {
        public String selectRecordDetail(Long depaId, Integer year, Integer month) {
            return new SQL() {{
                SELECT("asset_id as id, td.depr_date, td.depr_amount");
                FROM("tab_asset_depreciation td");
                if (year != null) {
                    WHERE("year(td.depr_date) = #{year}").AND()
                            .WHERE("month(td.depr_date) = #{month}");
                }
            }}.toString();
        }
    }
}
